/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package util.tokenization_io;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Utility class for reading in an entire file into a String. In principle, this
 * is the same as swutils.In.readAll() (using Sedgewick &amp; Wayne classes) but
 * the latter is too strict with faulty utf8.
 *
 * @author Dale Gerdemann
 */
public class Slurp {

    /**
     * @param filename the file to read from
     * @return the string contents of filename
     */
    public static String slurp(String filename) {
        FileReader f = null;

        try {
            f = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println(e.toString());
        }
        BufferedReader b = null;

        try {
            b = new BufferedReader(f);
        } catch (Exception e) {
            System.err.println(e.toString());
        }

        StringBuilder sb = new StringBuilder();
        while (true) {
            String line = "";
            try {
                line = b.readLine();
            } catch (IOException e) {
                System.err.println(e.toString());
                break;
            }
            if (line == null) {
                break;
            }
            sb.append(line).append("\n");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        try {
            f.close();
        } catch (Exception e) {
            System.err.println("File: " + f + " could not be closed");

        }
        return sb.toString();
    }

    /**
     * Improved slurp.
     *
     * @param filename
     * @return
     */
    public static String slurpIt(String filename) {

        Scanner s = null;
        try {
            s = new Scanner(new File(filename), "UTF-8");  // sonst funktioniert latin1

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Slurp.class.getName()).log(Level.SEVERE, null, ex);
        }

        StringBuilder sb = new StringBuilder();
        while (s.hasNextLine()) {
            String aLine = s.nextLine();
            sb.append(aLine).append("\n");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    /**
     * http://stackoverflow.com/questions/2056221/recursively-list-files-in-java
     *
     * @param path
     */
    public static void collectTextFiles(ArrayList<String> textFilesList, String path) {

        File root = new File(path);
        File[] list = root.listFiles();

        if (list == null) {
            return;
        }

        for (File f : list) {
            if (f.isDirectory()) {
                collectTextFiles(textFilesList, f.getAbsolutePath());
                //System.out.println( "Dir:" + f.getAbsoluteFile() );
            } else {
                String filename = f.getName();
                if (!filename.endsWith("~") // Editors (backup)
                        && !filename.matches("#\\.*#") // Editors
                        && !filename.matches("\\.#.*") // Editors
                        && !filename.matches("%\\.*%") // Editors
                        && !filename.equals("CVS") // CVS metadata
                        && !filename.equals(".cvsignore") // CVS metadata
                        && !filename.equals("sccs") // SCCS metadata
                        && !filename.equals("vssver.scc") // Microsoft Visual
                        && !filename.matches("\\._.*") // Mac OS/X
                        && !filename.equals(".svn") // Subversion
                        && !filename.equals(".DS_Store") // Mac OS/X
                        && !filename.equals("README")
                        && !filename.equals("readme")
                        && !filename.equals(".pdf")
                        && !filename.equals(".tar")
                        && !filename.equals(".gz")
                        && !filename.equals(".bz2")
                        && !filename.equals("loadOptions")
                        && !filename.equals("edits")
                        && !filename.equals(".ser")
                        && !filename.equals("serialize")) {
                    textFilesList.add(f.getAbsolutePath());
                    //System.out.println( "File:" + f.getAbsoluteFile() );
                }
            }
        }
    }
}
